﻿#region Copyright ©2008-2011, Technische Universitaet Muenchen
// ====================================================================================================
//
// Last Changed by       $Author: kalus $
// Last Changed Date     $LastChangedDate: 2009-07-29 14:54:57 +0200 (Wed, 29 Jul 2009) $
// Last Changed Revision $Rev: 258 $
//
// ====================================================================================================
#endregion

using System.Windows;
using System.Windows.Media;
using System.Diagnostics;
using System.IO;

namespace Tum.CollabXT.SharePoint
{
    /// <summary>
    /// Interaction logic for SharePointConfigPage.xaml
    /// </summary>
    public partial class SharePointConfigPage
    {
        #region Private Attributes
        private IConversionWorkflow _conversionWorkflow;
        private SharePointToolProvider _toolProvider;
        #endregion

        public SharePointConfigPage()
        {
            InitializeComponent();
            Loaded += OnLoaded;
            Unloaded += OnUnloaded;
        }

        private void OnLoaded(object sender, RoutedEventArgs e)
        {
            Debug.Assert(_toolProvider != null);

            if (Microsoft.SharePoint.Administration.SPWebService.ContentService == null)
            {
                lblStatusMessages.Foreground =
                   new SolidColorBrush(Color.FromArgb(255, 255, 0, 0));
                lblStatusMessages.Text = SharePoint.Resources.Language.MESSAGE_ERROR_NOT_ON_SHAREPOINT;
            }
            else
            {
                foreach (Microsoft.SharePoint.Administration.SPWebApplication webApp
                   in Microsoft.SharePoint.Administration.SPWebService.ContentService.WebApplications)
                {
                    foreach (Microsoft.SharePoint.SPSite site in webApp.Sites)
                        cbxWebApplication.Items.Add(site.Url);
                }

                if (!string.IsNullOrEmpty(_toolProvider.WebApplication))
                {
                    var site = new Microsoft.SharePoint.SPSite(_toolProvider.WebApplication);
                     cbxWebApplication.SelectedItem = _toolProvider.WebApplication;

                     if (!string.IsNullOrEmpty(_toolProvider.ParentSite))
                     {
                         Microsoft.SharePoint.SPWeb web = site.OpenWeb(_toolProvider.ParentSite);
                         if (web != null)
                         {
                             cbxParentSite.SelectedItem = web.ServerRelativeUrl;
                         }
                     } 
                }
            }

            if (string.IsNullOrEmpty(_toolProvider.TemplateDirectory) ||
                !Directory.Exists(_toolProvider.TemplateDirectory))
            {
                lblTemplateDirectoryValue.Content = SharePoint.Resources.Language.PathNotSet;
            }
            else
            {
                lblTemplateDirectoryValue.Content = _toolProvider.TemplateDirectory;
                lblTemplateDirectoryValue.Foreground = new SolidColorBrush(Color.FromRgb(0, 0, 0));
            }
        }

        private void OnUnloaded(object sender, RoutedEventArgs e)
        {
            _toolProvider.WebApplication = (string)cbxWebApplication.SelectedValue;
            _toolProvider.ParentSite = (string)cbxParentSite.SelectedValue;
            _toolProvider.SiteName = tbSiteName.Text;
            _toolProvider.SiteTitle = tbSiteTitle.Text;
        }

        public override void ReceiveProviderHandle(IToolProvider toolProvider)
        {
            _toolProvider = toolProvider as SharePointToolProvider;

            //Disable config page if SharePoint dll is not loaded
            if (!_toolProvider.SharePointAssemblyLoaded)
            {
                //Unregister SharePoint handlers
                Loaded -= OnLoaded;
                Unloaded -= OnUnloaded;

                //Disable all elements
                this.IsEnabled = false;
            }
        }

        public override void ReceiveWorkflowHandle(IConversionWorkflow workflow)
        {
            _conversionWorkflow = workflow;

            UpdateWorkflowButtons();
        }

        private void UpdateWorkflowButtons()
        {
            if (_toolProvider != null)
            {
                _conversionWorkflow.UpdateButtonState(true,
                    !string.IsNullOrEmpty((string)cbxWebApplication.SelectedValue) &&
                    !string.IsNullOrEmpty((string)cbxParentSite.SelectedValue) &&
                    !string.IsNullOrEmpty(tbSiteTitle.Text) &&
                    !string.IsNullOrEmpty(tbSiteName.Text) &&
                    !string.IsNullOrEmpty(_toolProvider.TemplateDirectory));
            }
            else
            {
                _conversionWorkflow.UpdateButtonState(true, false);
            }
        }

        private void OnInputControlsChanged(object sender, RoutedEventArgs e)
        {
            UpdateWorkflowButtons();
        }

        private void OnWebApplicationChanged(object sender, RoutedEventArgs e)
        {
            cbxParentSite.Items.Clear();
            var site = new Microsoft.SharePoint.SPSite((string)cbxWebApplication.SelectedValue);
            foreach (Microsoft.SharePoint.SPWeb web in site.AllWebs)
                cbxParentSite.Items.Add(web.ServerRelativeUrl);

            UpdateWorkflowButtons();
        }

        private void OnSetTemplateDirectory(object sender, RoutedEventArgs e)
        {
            var fbd = new System.Windows.Forms.FolderBrowserDialog {ShowNewFolderButton = false};

           if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                _toolProvider.TemplateDirectory = fbd.SelectedPath;
                lblTemplateDirectoryValue.Content = _toolProvider.TemplateDirectory;
                lblTemplateDirectoryValue.Foreground = new SolidColorBrush(Color.FromRgb(0, 0, 0));
            }

            UpdateWorkflowButtons();
        }
    }
}
